package com.yixiang.np.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yixiang.np.constant.GlobalConstant;
import com.yixiang.np.dao.DictionaryDao;
import com.yixiang.np.dao.DictionarytypeDao;
import com.yixiang.np.model.PageFilter;
import com.yixiang.np.model.po.Dictionary;
import com.yixiang.np.service.DictionaryServiceI;


@Service
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
public class DictionaryServiceImpl implements DictionaryServiceI {

	@Autowired
	private DictionaryDao dictionaryDao;
	 

	@Override
	public List<Dictionary> dataGrid(Dictionary dictionary, PageFilter ph) {
		List<Dictionary> ul = new ArrayList<Dictionary>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("dictionarytypeId", dictionary.getDictionarytypeId());
		ul = dictionaryDao.findAll(params);
		return ul;
	}

	@Override
	public Long count(Dictionary dictionary, PageFilter ph) {
		return dictionaryDao.count();
	}

	@Override
	public void add(Dictionary t) {
		t.setIsdefault(GlobalConstant.NOT_DEFAULT);
		t.setState(GlobalConstant.ENABLE);
		dictionaryDao.add(t);
	}

	@Override
	public void delete(Long id) {
		dictionaryDao.delete(id);
	}

	@Override
	public void edit(Dictionary dictionary) {
		dictionaryDao.edit(dictionary);
	}

	@Override
	public Dictionary get(Long id) {
		return dictionaryDao.get(id);
	}

	@Override
	public List<Dictionary> combox(String code) {
		List<Dictionary> ld = new ArrayList<Dictionary>();
		List<Dictionary>  lt = dictionaryDao.findByCode(code);
		if(lt!=null&&lt.size()>0){
			 for (int i = 0; i < lt.size(); i++) {
				 if(lt.get(i).getState()==0){
					 Dictionary d = new Dictionary();
					 d.setId(lt.get(i).getId());
					 d.setText(lt.get(i).getText());
					 ld.add(d);
				 }
			}
		 }
		return ld;
	}

	@Override
	public Dictionary checkUnique(Dictionary dictionary) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("code", dictionary.getCode());
		params.put("dictionarytype_id", dictionary.getDictionarytypeId());
		Dictionary t = dictionaryDao.checkUnique(params);
		if(t!=null){
			return t;
		}else{
			return null;
		}
	}
 

}
